#include "HUD.h"
#include "Constants.h"
#include "base\Engine.h"
#include "game\World.h"
#include "renderer\DebugRenderer.h"

const ds::Rect FONT_RECT = ds::Rect(150,0,26,20);

const float SCALING = 1.5f;
const float SCALE_MULTIPLIER = 1.0f;
const float HS_SCALING = 2.0f;

// ---------------------------------------------------------------
//  HUDCounter
// ---------------------------------------------------------------
HUDCounter::HUDCounter(ds::SpriteNode* sprites,const ds::Vec2& textPos,const ds::Rect& textRect,const ds::Vec2& pos,int digits,const ds::Color& col,bool active) 
	: m_Sprites(sprites) {
	create(textPos,textRect,pos,FONT_RECT,digits,col,active);
}

HUDCounter::HUDCounter(ds::SpriteNode* sprites,const ds::Vec2& textPos,const ds::Rect& textRect,const ds::Vec2& pos,const ds::Rect& fontRect,int digits,const ds::Color& col,bool active) 
	: m_Sprites(sprites) {
		create(textPos,textRect,pos,fontRect,digits,col,active);
}

void HUDCounter::updateScaling(float elapsed) {
	scaleTimer += elapsed * SCALE_MULTIPLIER;		
	float t = SCALING - ds::math::smoothstep(scaleTimer/SCALING)*SCALING;
	if ( t < 1.0f ) {
		scaled = false;
		t = 1.0f;
	}
	spriteCounter->setScale(ds::Vec2(t,t));
}

void HUDCounter::create(const ds::Vec2& textPos,const ds::Rect& textRect,const ds::Vec2& pos,const ds::Rect& fontRect,int digits,const ds::Color& col,bool active) {
	spriteCounter = new ds::SpriteCounter(m_Sprites,fontRect,fontRect.width(),digits);
	spriteCounter->setCounter(0);
	spriteCounter->setPos(pos);
	spriteCounter->setActive(active);
	spriteCounter->setColor(col);

	counterText = m_Sprites->add(textPos,textRect);
	m_Sprites->setColor(counterText,col);
	m_Sprites->setVisible(counterText,active);
	scaled = false;
	scaleTimer = 0.0f;
	scaling = 1.0f;	
}
// ---------------------------------------------------------------
//  HUD
// ---------------------------------------------------------------
void HUD::init() {
	// text
	addStaticImage(ds::Vec2(130,50),ds::Rect(180,0,115,20),ds::Color(1.0f,0.5f,0.0f,1.0f));
	addStaticImage(ds::Vec2(410,50),ds::Rect(218,0,190,20),ds::Color(1.0f,0.5f,0.0f,1.0f));
	addStaticImage(ds::Vec2(780,50),ds::Rect(199,0,90,18) ,ds::Color(1.0f,0.5f,0.0f,1.0f));
	// percentage
	addStaticImage(ds::Vec2(515,91),ds::Rect(180,125,37,20) ,ds::Color(0.0f,0.5f,1.0f,1.0f));
	// :
	addStaticImage(ds::Vec2(815,92),ds::Rect(180,170,9,20) ,ds::Color(0.0f,0.5f,1.0f,1.0f));

	m_ScoreCounter = new ds::SpriteCounter(this,ds::Vec2(110,90),FONT_RECT,6,ds::Color(0.0f,0.5f,1.0f,1.0f));
	m_CoverageCounter = new ds::SpriteCounter(this,ds::Vec2(460,90),FONT_RECT,2,ds::Color(0.0f,0.5f,1.0f,1.0f));
	m_MinCounter = new ds::SpriteCounter(this,ds::Vec2(760,90),FONT_RECT,2,ds::Color(0.0f,0.5f,1.0f,1.0f));
	m_SecCounter = new ds::SpriteCounter(this,ds::Vec2(828,90),FONT_RECT,2,ds::Color(0.0f,0.5f,1.0f,1.0f));
	
	reset();
	m_Timing = true;
}

ds::SHandle HUD::addStaticImage(const ds::Vec2& pos,const ds::Rect& textureRect,const ds::Color& color,bool active) {
	ds::SHandle handle =add(pos,textureRect);
	setColor(handle,color);
	setVisible(handle,active);
	return handle;
}

void HUD::update(float elapsed) {
	/*
	if ( m_Running ) {
		if ( m_ScoreCounter.isScaled() ) {
			m_ScoreCounter.updateScaling(elapsed);
		}
		//if ( m_HighscoreCounter.isScaled() ) {
			//m_HighscoreCounter.updateScaling(elapsed);
		//}
		if ( m_Timing ) {
			m_Timer += elapsed;
			if ( m_Timer >= 1.0f ) {
				--m_Seconds;
				m_Timer = 0.0f;
				m_TimerCounter.setValue(m_Seconds);
				if ( m_Seconds <= 0 ) {
					m_Timing = false;
					FIRE_EMPTY_EVENT(EVN_GAME_OVER);				
				}
			}
		}
	}
	*/
	if ( m_Timing ) {
		m_Timer += elapsed;
		if ( m_Timer >= 1.0f ) {
			++m_Seconds;
			m_Timer = 0.0f;			
			if ( m_Seconds >= 60 ) {
				m_Seconds = 0;
				++m_Minutes;
			}
			m_SecCounter->setCounter(m_Seconds);
			m_MinCounter->setCounter(m_Minutes);
			if ( m_TTL != 0 ) {
				int sec = m_Seconds + m_Minutes * 60;
				if ( sec >= m_TTL ) {
					FIRE_EMPTY_EVENT(EVN_TIME_ELAPSED);
				}
			}
		}
	}
}

void HUD::activate() {
	m_Running = true;
	//m_ScoreCounter.setScaleMode(false);
	//m_ScoreCounter.setScaling(1.0f);
	//m_ScoreCounter.setValue(0);
}

void HUD::deactivate() {
	m_Running = false;
}

void HUD::setScore(int value) {
	m_ScoreCounter->setCounter(value);
	//m_ScoreCounter.setScaleMode(true);
	//m_ScoreCounter.setScaling(SCALING);
	/*
	if ( value > m_HighscoreCounter.getValue() ) {
		m_HighscoreCounter.setValue(value);
		m_HighscoreCounter.setScaleMode(true);
		m_HighscoreCounter.setScaling(SCALING);
	}
	*/	
}

void HUD::process(ds::EventBuffer& buffer) {
	/*
	if ( buffer.containsID(EVN_ADD_SCORE)) {
		FinalScore fs;
		buffer.getByID(&fs,sizeof(FinalScore),EVN_ADD_SCORE);
		setScore(fs.points);
	}
	else if ( buffer.containsID(EVN_RESET_GAME) ) {
		reset();
	}
	else if ( buffer.containsID(EVN_SETTINGS_CHANGED)) {
		GameSettings gs;
		buffer.getByID(&gs,sizeof(GameSettings),EVN_SETTINGS_CHANGED);
		m_Level = gs.level;
		m_TTL = gs.ttl;
		m_Timing = gs.rushMode;
		//m_HighscoreCounter.setValue(m_HighScore->getScore(m_Level,m_TTL));		 		
		if ( m_Timing ) {
			m_TimerCounter.setValue(m_TTL);
			//m_TimerCounter.setVisible(true);
			m_Seconds = m_TTL;
			m_Timer = 0.0f;
		}	
		else {
			//m_TimerCounter.setVisible(false);
			m_TTL = 0;
		}
	}
	else if ( buffer.containsID(EVN_GAME_OVER)) {
		FinalScore fs;
		buffer.getByID(&fs,sizeof(FinalScore),EVN_GAME_OVER);
		m_HighScore->setScore(fs.points,m_Level,m_TTL);
		m_HighScore->saveHighscores();
		//m_HighscoreCounter.setValue(m_HighScore->getScore(m_Level,m_TTL));
	}
	*/
}
